/*
 * search_ticket_left_cb.c
 *
 *  Created on: 2013-2-5
 *      Author: panqiuhui
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "../head/gui_callback/search_ticket_left_cb.h"
#include "../head/gui_callback/global_cb.h"
#include "../head/communicate_with_server/traininfo_transfer.h"
#include "../head/common_struct/traininfo.h"

extern GtkBuilder *builder;

enum {
	SEARCH_TICKET_LEFT_COL_TRAINID = 0,
	SEARCH_TICKET_LEFT_COL_ISFIRST,
	SEARCH_TICKET_LEFT_COL_STARTSTATION,
	SEARCH_TICKET_LEFT_COL_STARTTIME,
	SEARCH_TICKET_LEFT_COL_ISLAST,
	SEARCH_TICKET_LEFT_COL_ENDSTATION,
	SEARCH_TICKET_LEFT_COL_ENDTIME,
	SEARCH_TICKET_LEFT_COL_TIMELAST,
	SEARCH_TICKET_LEFT_COL_TICKETINFO,
	SEARCH_TICKET_LEFT_COL_DEPARTDATE,
	SEARCH_TICKET_LEFT_COL_PRICE,
};

//处理工具栏中的查询余票的点击事件
void toolbutton_search_ticket_left_clicked_cb(GtkToolButton *toolbutton,
		gpointer user_data) {
	GtkEntry *entry_search_ticket_left_startaddress;
	GtkEntry *entry_search_ticket_left_endaddress;
	GtkEntry *entry_search_ticket_left_train;
	GtkEntry *entry_search_ticket_left_startdate;
	GtkCalendar *calendar_search_ticket_left_startdate;
	GtkComboBox *combobox_search_ticket_left_begintime;
	GtkRadioButton *radiobutton_search_ticket_left_stationtype_all;
	GtkRadioButton *radiobutton_search_ticket_left_stationtype_start;
	GtkRadioButton *radiobutton_search_ticket_left_stationtype_pass;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_DG;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_Z;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_T;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_K;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_other;
	GtkTreeView *treeview_search_ticket_left;

	entry_search_ticket_left_startaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_startaddress"));
	entry_search_ticket_left_endaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_endaddress"));
	entry_search_ticket_left_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_train"));
	entry_search_ticket_left_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_startdate"));
	calendar_search_ticket_left_startdate =
			GTK_CALENDAR(
					gtk_builder_get_object(builder, "calendar_search_ticket_left_startdate"));
	combobox_search_ticket_left_begintime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_search_ticket_left_begintime"));
	radiobutton_search_ticket_left_stationtype_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_ticket_left_stationtype_all"));
	radiobutton_search_ticket_left_stationtype_start =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_ticket_left_stationtype_start"));
	radiobutton_search_ticket_left_stationtype_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_ticket_left_stationtype_pass"));
	checkbutton_search_ticket_left_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_DG"));
	checkbutton_search_ticket_left_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_Z"));
	checkbutton_search_ticket_left_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_T"));
	checkbutton_search_ticket_left_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_K"));
	checkbutton_search_ticket_left_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_other"));
	treeview_search_ticket_left = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_search_ticket_left"));

	gtk_entry_set_text(entry_search_ticket_left_startaddress, "");
	gtk_entry_set_text(entry_search_ticket_left_endaddress, "");
	gtk_entry_set_text(entry_search_ticket_left_train, "");

	struct tm *tm_ptr;
	time_t the_time;
	(void) time(&the_time);
	tm_ptr = localtime(&the_time);
	char date[TRAIN_NUMBER_DATE_LEN];
	memset(date, 0, sizeof(date));
	sprintf(date, "%04d-%02d-%02d", tm_ptr->tm_year + 1900, tm_ptr->tm_mon + 1,
			tm_ptr->tm_mday);
	gtk_entry_set_text(entry_search_ticket_left_startdate, date);
	gtk_calendar_select_month(calendar_search_ticket_left_startdate,
			tm_ptr->tm_mon, tm_ptr->tm_year + 1900);
	gtk_calendar_select_day(calendar_search_ticket_left_startdate,
			tm_ptr->tm_mday);

	gtk_combo_box_set_active(combobox_search_ticket_left_begintime, 0);

	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_search_ticket_left_stationtype_all
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_search_ticket_left_stationtype_start
			), false);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(radiobutton_search_ticket_left_stationtype_pass
			), false);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_ticket_left_traintype_DG
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_ticket_left_traintype_Z
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_ticket_left_traintype_T
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_ticket_left_traintype_K
			), true);
	gtk_toggle_button_set_active(
			GTK_TOGGLE_BUTTON(checkbutton_search_ticket_left_traintype_other
			), true);

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_ticket_left);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	window_show(SEARCH_TICKET_LEFT);
}

//处理余票查询按钮的点击事件
void button_search_ticket_left_clicked_cb(GtkButton *button, gpointer user_data) {
	GtkEntry *entry_search_ticket_left_startaddress;
	GtkEntry *entry_search_ticket_left_endaddress;
	GtkEntry *entry_search_ticket_left_train;
	GtkEntry *entry_search_ticket_left_startdate;
	GtkComboBox *combobox_search_ticket_left_begintime;
	GtkRadioButton *radiobutton_search_ticket_left_stationtype_all;
	GtkRadioButton *radiobutton_search_ticket_left_stationtype_start;
	GtkRadioButton *radiobutton_search_ticket_left_stationtype_pass;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_DG;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_Z;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_T;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_K;
	GtkCheckButton *checkbutton_search_ticket_left_traintype_other;
	GtkTreeView *treeview_search_ticket_left;
	GtkWindow *global_window;

	global_window =
			GTK_WINDOW(gtk_builder_get_object(builder, "global_window"));
	entry_search_ticket_left_startaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_startaddress"));
	entry_search_ticket_left_endaddress =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_endaddress"));
	entry_search_ticket_left_train =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_train"));
	entry_search_ticket_left_startdate =
			GTK_ENTRY(gtk_builder_get_object(builder,"entry_search_ticket_left_startdate"));
	combobox_search_ticket_left_begintime =
			GTK_COMBO_BOX(gtk_builder_get_object(builder, "combobox_search_ticket_left_begintime"));
	radiobutton_search_ticket_left_stationtype_all =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_ticket_left_stationtype_all"));
	radiobutton_search_ticket_left_stationtype_start =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_ticket_left_stationtype_start"));
	radiobutton_search_ticket_left_stationtype_pass =
			GTK_RADIO_BUTTON(gtk_builder_get_object(builder,"radiobutton_search_ticket_left_stationtype_pass"));
	checkbutton_search_ticket_left_traintype_DG =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_DG"));
	checkbutton_search_ticket_left_traintype_Z =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_Z"));
	checkbutton_search_ticket_left_traintype_T =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_T"));
	checkbutton_search_ticket_left_traintype_K =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_K"));
	checkbutton_search_ticket_left_traintype_other =
			GTK_CHECK_BUTTON(gtk_builder_get_object(builder,"checkbutton_search_ticket_left_traintype_other"));
	treeview_search_ticket_left = GTK_TREE_VIEW(
			gtk_builder_get_object(builder, "treeview_search_ticket_left"));

	GtkTreeModel *model;
	model = gtk_tree_view_get_model(treeview_search_ticket_left);
	gtk_list_store_clear(GTK_LIST_STORE(model) );

	const gchar *startAddress = gtk_entry_get_text(
			entry_search_ticket_left_startaddress);
	const gchar *endAddress = gtk_entry_get_text(
			entry_search_ticket_left_endaddress);
	if (strlen(startAddress) == 0) {
		gtk_show_warning(global_window, "%s", "出发地不能为空", "输入提醒");
		return;
	}

	if (strlen(endAddress) == 0) {
		gtk_show_warning(global_window, "%s", "目的地不能为空", "输入提醒");
		return;
	}

	const gchar *trainID = gtk_entry_get_text(entry_search_ticket_left_train);
	const gchar *departDate = gtk_entry_get_text(
			entry_search_ticket_left_startdate);

	const gchar *startTimeRange;
	GtkTreeModel *model_timerange = gtk_combo_box_get_model(
			combobox_search_ticket_left_begintime);
	GtkTreeIter iter_timerange;
	gtk_combo_box_get_active_iter(combobox_search_ticket_left_begintime,
			&iter_timerange);
	gtk_tree_model_get(model_timerange, &iter_timerange, 0, &startTimeRange,
			-1);
	int startTimeRangeLowHour;
	int startTimeRangeLowMinute;
	int startTimeRangeHighHour;
	int startTimeRangeHighMinute;
	sscanf(startTimeRange, "%02d:%02d--%02d:%02d", &startTimeRangeLowHour,
			&startTimeRangeLowMinute, &startTimeRangeHighHour,
			&startTimeRangeHighMinute);

	int passStationType = TYPE_PASS_STATION_ALL;
	gboolean isPassStationTypeALL = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_ticket_left_stationtype_all) );
	if (isPassStationTypeALL) {
		passStationType = TYPE_PASS_STATION_ALL;
	}
	gboolean isPassStationTypeBegin = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_ticket_left_stationtype_start) );
	if (isPassStationTypeBegin) {
		passStationType = TYPE_PASS_STATION_BEGIN;
	}
	gboolean isPassStationTypePass = gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
					radiobutton_search_ticket_left_stationtype_pass) );
	if (isPassStationTypePass) {
		passStationType = TYPE_PASS_STATION_PASS;
	}

	int trainType = TYPE_TRAIN_TYPE_NONE;
	gboolean isTrainTypeDG = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_ticket_left_traintype_DG) );
	if (isTrainTypeDG) {
		trainType += TYPE_TRAIN_TYPE_DG;
	}
	gboolean isTrainTypeZ = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_ticket_left_traintype_Z) );
	if (isTrainTypeZ) {
		trainType += TYPE_TRAIN_TYPE_Z;
	}
	gboolean isTrainTypeT = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_ticket_left_traintype_T) );
	if (isTrainTypeT) {
		trainType += TYPE_TRAIN_TYPE_T;
	}
	gboolean isTrainTypeK = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_ticket_left_traintype_K) );
	if (isTrainTypeK) {
		trainType += TYPE_TRAIN_TYPE_K;
	}
	gboolean isTrainTypeOther = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			checkbutton_search_ticket_left_traintype_other) );
	if (isTrainTypeOther) {
		trainType += TYPE_TRAIN_TYPE_OTHER;
	}

	if (trainType == TYPE_TRAIN_TYPE_NONE) {
		gtk_show_warning(global_window, "%s", "请选择列车类型", "输入提醒");
		return;
	}

	struct ticket_left_request request;
	memset(&request, 0, sizeof(request));
	g_utf8_strncpy(request.startAddress, startAddress,
			g_utf8_strlen(startAddress, STATIONS_CITY_LEN));
	g_utf8_strncpy(request.endAddress, endAddress,
			g_utf8_strlen(endAddress, STATIONS_CITY_LEN));
	strncpy(request.trainID, trainID, strlen(trainID));
	strncpy(request.departDate, departDate, strlen(departDate));
	sprintf(request.timeRangeLow, "%02d:%02d", startTimeRangeLowHour,
			startTimeRangeLowMinute);
	sprintf(request.timeRangeHigh, "%02d:%02d", startTimeRangeHighHour,
			startTimeRangeHighMinute);
	request.passStationType = passStationType;
	request.trainType = trainType;

	TicketList ticketlist; //声明列表
	InitializeListTicket(&ticketlist); //初始化列表
	bool search_result = search_ticket_left(&request, &ticketlist);
	if (search_result) {
		char departStation[STATIONS_STATION_NAME_LEN] = "first.gif";
		char arriveStation[STATIONS_STATION_NAME_LEN] = "last.gif";
		char passStation[STATIONS_STATION_NAME_LEN] = "pass.png";
		char ticketNone[10] = "无";
		char ticketNoStartSale[10] = "*";

		char isfirst[STATIONS_STATION_NAME_LEN];
		char islast[STATIONS_STATION_NAME_LEN];
		char ticketinfo[10];
		memset(isfirst, 0, sizeof(isfirst));
		memset(islast, 0, sizeof(islast));
		strncpy(isfirst, passStation, strlen(passStation));
		strncpy(islast, passStation, strlen(passStation));
		memset(ticketinfo, 0, sizeof(ticketinfo));
		TicketNode *node = ticketlist;
		GtkTreeIter iter;
		while (node != NULL ) {
			TicketItem item = node->item;
			if (item.isDepartStation) {
				memset(isfirst, 0, sizeof(isfirst));
				strncpy(isfirst, departStation, strlen(departStation));
			}
			if (item.isArriveStation) {
				memset(islast, 0, sizeof(islast));
				strncpy(islast, arriveStation, strlen(arriveStation));
			}
			if (item.isStartSale) {
				if (item.ticketLeft == 0) {
					sprintf(ticketinfo, "%s", ticketNone);
				} else {
					sprintf(ticketinfo, "%d", item.ticketLeft);
				}
			} else {
				sprintf(ticketinfo, "%s", ticketNoStartSale);
			}

			GdkPixbuf* icon_first = gdk_pixbuf_new_from_file(isfirst, NULL );
			GdkPixbuf* icon_last = gdk_pixbuf_new_from_file(islast, NULL );

			gtk_list_store_append(GTK_LIST_STORE(model), &iter);
			gtk_list_store_set(GTK_LIST_STORE(model), &iter,
					SEARCH_TICKET_LEFT_COL_TRAINID, item.trainID,
					SEARCH_TICKET_LEFT_COL_ISFIRST, icon_first,
					SEARCH_TICKET_LEFT_COL_STARTSTATION, item.startStation,
					SEARCH_TICKET_LEFT_COL_STARTTIME, item.startTime,
					SEARCH_TICKET_LEFT_COL_ISLAST, icon_last,
					SEARCH_TICKET_LEFT_COL_ENDSTATION, item.endStation,
					SEARCH_TICKET_LEFT_COL_ENDTIME, item.endTime,
					SEARCH_TICKET_LEFT_COL_TIMELAST, item.consumeTime,
					SEARCH_TICKET_LEFT_COL_TICKETINFO, ticketinfo,
					SEARCH_TICKET_LEFT_COL_DEPARTDATE, item.departDate,
					SEARCH_TICKET_LEFT_COL_PRICE, item.price, -1);

			node = node->next;
		}
	} else {
		gtk_show_info(global_window, "%s", "未找到相应信息", "信息提示");
	}
	EmptyTheListTicket(&ticketlist);
}
